package com.wx.wxsecondskill.service;

import com.wx.redis.RedisUtils;
import com.wx.wxsecondskill.po.OrderInfo;
import com.wx.wxsecondskill.po.User;
import com.wx.wxsecondskill.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.wx.wxsecondskill.controller.SecondsKillController.USERSECONDSKILREFIX;

/**
 * @ClassName SecondsKillService
 * @Author wx
 * @Description 秒杀操作业务层
 * @Date 2018-11-15-22:17
 */
@Service
public class SecondsKillService {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    /**
     * @methodName: miaosha
     * @author: wx
     * @description: 进行秒杀
     * @param goods
     * @date: 2018/11/15
     * @return: com.wx.wxsecondskill.po.OrderInfo
     */
    @Transactional
    public OrderInfo miaosha(User user, GoodsVo goods) {
        //减库存 下订单 写入秒杀订单
        goodsService.reduceStock(goods);
        //创建订单
        return orderService.createOrder(user,goods);
    }

    /**
     * @methodName: miaosha
     * @author: wx
     * @description: 进行秒杀
     * @param goods
     * @date: 2018/11/19
     * @return: com.wx.wxsecondskill.po.OrderInfo
     */
    @Transactional
    public OrderInfo miaoshV2(User user, GoodsVo goods) {
        //减库存 下订单 写入秒杀订单
        goodsService.reduceStock(goods);
        //创建订单
        OrderInfo orderInfo = orderService.createOrder(user, goods);
        //设置用户秒杀该商品的记录缓存
        RedisUtils.redisClient.set(USERSECONDSKILREFIX + user.getId() + "-" + goods.getId(), true, 300);
        return orderInfo;
    }
}
